﻿@{
    ViewBag.Title = "Index";
    Layout = null;
}

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <meta charset="UTF-8">
        <title>派送统计报表</title>
        <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/default/easyui.css">
        <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/icon.css">
        <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/demo.css">
        <link href="~/Content/Styles/Order.css" rel="stylesheet" />
        <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.min.js"></script>
        <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
        <script src="~/Content/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>

        <style>
            .dataTable {
                border-collapse: collapse;
                border: 1px solid #666;
                /*border-right: 1px solid #666;
                border-bottom: 1px solid #666;*/
            }

                .dataTable td {
                    border: 1px solid #666;
                    /*border-left: 1px solid #666;
                    border-top: 1px solid #666;*/
                }

            .trCenter td {
                text-align: center;
            }
        </style>

        <script>
            $(function () {
                $("#TJDiv").hide();
                //商户下拉列表
                $.get("/StatisticalReportSend/getMer", {}, function (database) {
                    $('#Mers').combobox({
                        valueField: 'MerID',
                        textField: 'MerName',
                        editable: false,
                        data: database
                    });
                });

                //获取订单状态
                    $.get("/StatisticalReportSend/getOrderFormType", {}, function (database) {

                        var a = {
                            ConText: "全部",
                            ConID: -8989,
                        }

                        database.splice(0, 0, a);

                        $('#ConIDs').combobox({
                            textField: "ConText",
                            valueField: "ConID",
                            data: database,
                            multiple: true,
                            editable: false,
                            panelWidth:150,
                            formatter: function (row) {
                                //console.log(row);
                                var opts;
                                if (row.selected == true) {
                                    opts = "<input type='checkbox' checked='checked' id='" + row.ConID + "' value='" + row.ConID + "'>" + row.ConText + "</input>";
                                } else {
                                    opts = "<input type='checkbox' id='" + row.ConID + "' value='" + row.ConID + "'>" + row.ConText;
                                }
                                return opts;
                            },
                            panelHeight: "auto",
                            onSelect: function (rec) { // 选中一个选项
                                //console.log(rec);
                                oCheckbox = document.getElementById(rec.ConID);
                                //console.log(oCheckbox);
                                if (oCheckbox.id == -8989) { // -8989“全部”选项的id  
                                    for (var i = 0; i < ($("input:checkbox").length) ; i++) {  // html中有一个复选按钮所以长度-1
                                        //console.log(i);
                                        $("input:checkbox")[i].checked = true; // 复选框勾选
                                        $('#ConIDs').combobox('select', $("input:checkbox")[i].id);  // 选定下拉选项的值
                                    }
                                    return false;
                                }
                                oCheckbox.checked = true;
                                if ($("input[type='checkbox']:checked").length == ($("input:checkbox").length - 2)) {
                                    for (var i = 0; i < ($("input:checkbox").length) ; i++) {
                                        //console.log(i);
                                        $("input:checkbox")[i].checked = true;
                                        $('#ConIDs').combobox('select', $("input:checkbox")[i].id);
                                    }
                                    return false;
                                }
                            }, onUnselect: function (rec) { // 取消一个选项
                                oCheckbox = document.getElementById(rec.ConID);
                                var a = $("#ConIDs").combobox('getValues');
                                console.log(a);
                                if (oCheckbox.id == -8989) { // 是否是“全部”选项的id
                                    for (var i = 0; i < ($("input:checkbox").length) ; i++) {
                                        $("input:checkbox")[i].checked = false; // 复选框不勾选
                                        $('#ConIDs').combobox('clear', 'none'); // 清除组合框（combobox）的值。
                                    }
                                } else if (a.indexOf("-8989") >= 0) { // ”全部“选项是否处于选中状态
                                    oCheckbox.checked = false; // 
                                    document.getElementById("-8989").checked = false;
                                    var value = $("#ConIDs").combobox('getValues') // 获取所有值
                                    $('#ConIDs').combobox('clear', 'none'); // 清除组合框（combobox）的值。
                                    value.splice(jQuery.inArray('-8989', value), 1); // 在数组中删除“-8989”
                                    for (var i in value) {
                                        $('#ConIDs').combobox('select', value[i]);
                                    }
                                } else {
                                    oCheckbox.checked = false;
                                }
                            }


                        })
                    })

            })

            // 查询
            function Search() {
                var postData = {
                    MerID: $("#Mers").combobox("getValue"),
                    ConID: $("#ConIDs").combobox("getValues"),
                    TimeS: $("#TimeS").datebox("getValue"),
                    TimeE: $("#TimeE").datebox("getValue"),
                }
                console.log(postData.ConID);
                if (postData.MerID == "" || postData.MerID == null) {
                    $.messager.alert("友情提示", "请选择商户");
                    return false;
                }
                if (postData.ConID == "" || postData.ConID == null) {
                    $.messager.alert("友情提示", "请选择订单状态");
                    return false;
                }
                if (postData.ConID.indexOf("-8989") >= 0 || postData.ConID.length == 0) {
                    //console.log(postdata.FormControl);
                    postData.ConID.splice(jQuery.inArray('-8989', postData.ConID), 1);
                }
                console.log(postData.ConID);
                console.log(postData);
                //url: '/StatisticalReportSend/StatisticalReport?MerID=' + postData.MerID + "&ConID=" + postData.ConID + "&TimeS=" + postData.TimeS + "&TimeE=" + postData.TimeE,
                $.post('/StatisticalReportSend/StatisticalReport?MerID=' + postData.MerID + "&ConID=" + postData.ConID + "&TimeS=" + postData.TimeS + "&TimeE=" + postData.TimeE, function (resp) {
                    console.log(1);
                    //删除表格中所有行
                    var tb = document.getElementById('TJTable');
                    var rowNum = tb.rows.length;
                    for (i = 0; i < rowNum; i++) {
                        tb.deleteRow(i);
                        rowNum = rowNum - 1;
                        i = i - 1;
                    }
                    var coun = 0;
                    $("#TJDiv").show();
                    $('#TJTable').append("<tr><td colspan='3'  style='text-align: center; font-size: 20px;'>派送费统计报表</td></tr>"+
                            "<tr><td>商户名称：" + $('#Mers').combobox('getText') + "</td><td colspan='2'>时间：" + $('#TimeS').textbox('getValue') + "至" + $('#TimeE').textbox('getValue') + "</td></tr>" +
                            "<tr class='trCenter'><td>名称</td><td>订单数量</td><td>派送费总费</td></tr>");
                    for (var i in resp.rows) {
                        coun = coun + resp.rows[i].CountForm;
                        $('#TJTable').append("<tr class='trCenter'><td>" + resp.rows[i].StatName + "订单总数</td><td>" + resp.rows[i].CountForm + "份</td><td>" + resp.rows[i].Count / 100 + "元</td></tr>");
                    }
                    $('#TJTable').append("<tr class='trCenter'><td>总计</td><td>"+ coun +"份</td><td>" + resp.total / 100 + "元</td></tr>");
                });
            }

            // 打印
            function preview() {
                bdhtml = window.document.body.innerHTML;
                sprnstr = "<!--startprint-->";
                eprnstr = "<!--endprint-->";
                prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
                prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
                window.document.body.innerHTML = prnhtml;
                window.print();
                window.location.href = "/StatisticalReportSend/Index";
            }

            // 打印
            //function preview() {
            //    mywindow = window.open("", 'width=200,height=300')
            //    bdhtml = window.document.body.innerHTML;
            //    sprnstr = "<!--startprint-->";
            //    eprnstr = "<!--endprint-->";
            //    prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
            //    prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
            //    mywindow.document.write(prnhtml);
            //    mywindow.print();
            //    //window.document.body.innerHTML = prnhtml;
            //    //window.print();
            //}
        </script>
    </head>
    <body>
        <div style="text-align: center; margin: 20px 0">
            <div style="margin-bottom: 10px; display:inline-block">
                商户：&nbsp;
            <input id="Mers" class="easyui-combobox" style="width: 120px;">&nbsp;订单状态：&nbsp;
                <input id="ConIDs" class="easyui-combobox" style="width: 120px;" />
                &nbsp;开始时间:&nbsp;
            <input id="TimeS" class="easyui-datebox" style="width: 120px">&nbsp;
            到：
            <input id="TimeE" class="easyui-datebox" style="width: 120px">
            </div>&nbsp;&nbsp;&nbsp;&nbsp;  <a href="#" class="easyui-linkbutton" onclick="Search()" iconcls="icon-search">查询</a>
        </div>
        <div id="TJDiv" style="margin-top: 50px;">
            <!--startprint-->
        <table id="TJTable" class="dataTable" style="width: 90%; margin: 0 auto; margin-bottom: 30px;">
            @*<tr>
                <td colspan="3"  style='text-align: center; font-size: 20px;'>派送费统计报表</td>
            </tr>
            <tr>
                <td>商户名称：西苑餐厅</td>
                <td colspan="2">时间：2016-12-01至2016-12-14</td>
            </tr>
            <tr>
                <td>名称</td>
                <td>订单数量</td>
                <td>派送费总费</td>
            </tr>
            <tr>
                <td>已完成订单</td>
                <td>10</td>
                <td>5</td>
            </tr>*@
        </table>
            <!--endprint-->
        <div style="text-align: center">
            <a href="#" class="easyui-linkbutton" iconcls="icon-print" onclick="preview()" >打印</a>
        </div>
    </div>
    </body>
</html>
